import { ConfigEnv, defineConfig, loadEnv } from "vite";
import path from "path";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import uni from "@dcloudio/vite-plugin-uni";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import h5ProdEffectPlugin from "uni-vite-plugin-h5-prod-effect";

// https://vitejs.dev/config/
export default defineConfig((mode: ConfigEnv) => {
  const env = loadEnv(mode.mode, process.cwd());
  return {
    plugins: [
      uni(),
      h5ProdEffectPlugin(),

      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), "src/static/icons")],
        // 指定symbolId格式
        symbolId: "icon-[dir]-[name]",
      }),
      AutoImport({
        resolvers: [],
        imports: [
          "vue",
          "vue-router",
          "vue/macros",
          "pinia",
          "@vueuse/head",
          "@vueuse/core",
          {
            "@/hooks/usePage": ["usePage"],
            "@/router": [
              "navigate",
              "navigateBack",
              "useInit",
              "parseUrl",
              "getPageByName",
            ],
          },
        ],

        dts: "types/auto-imports.d.ts",
        eslintrc: {
          enabled: false, // Default `false`
          filepath: "./.eslintrc-auto-import.json", // Default `./.eslintrc-auto-import.json`
          globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
        },
      }),
      Components({ dts: true }),
    ],
    root: process.cwd(),
    base: mode.command === "serve" ? "./" : env.VITE_APP_PUBLIC_PATH,
    server: {
      host: "0.0.0.0",
      port: 5001,
      open: true,
      proxy: {
        "/dev-api": {
          target: "http://47.108.233.183/prd-dast",
          ws: true,
          changeOrigin: true,
          rewrite: (path) => path.replace(new RegExp(env.VITE_API_URL), ""),
        },
      },
    },
    build: {
      outDir: "dist",
      minify: "terser",
      sourcemap: false,
      chunkSizeWarningLimit: 1500,
      rollupOptions: {
        external: ["axios"],
        output: {
          entryFileNames: `assets/[name].${new Date().getTime()}.js`,
          chunkFileNames: `assets/[name].${new Date().getTime()}.js`,
          assetFileNames: `assets/[name].${new Date().getTime()}.[ext]`,
          compact: true,
          manualChunks: {
            vue: ["vue", "vue-router"],
            // echarts: ['echarts'],
          },
        },
      },
      terserOptions: {
        compress: {
          drop_console: false,
          drop_debugger: true,
        },
        ie8: true,
        output: {
          comments: true,
        },
      },
    },
    css: {
      postcss: {
        plugins: [
          {
            postcssPlugin: "internal:charset-removal",
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === "charset") {
                  atRule.remove();
                }
              },
            },
          },
        ],
      },
    },
  };
});
